#include <stdio.h>


# define IN 1
# define OUT 0
/*
 *  (1)单词计数，经典且简单的例子，注意状态的转换
 *  适合写个小状态机搞清楚再写代码，比较好培养工作方式
 *  但是！lin平台下ctl+D输入EOF，win下ctl+Z并不能退出，貌似是mingw问题，换cygwin可能解决
 */
void wordCount(){
    int c ,nl, nw, nc, state;
    nl = nw = nc =0;

    while ( (c=getchar()) != EOF ){
        nc++;
        if (c == '\n')
            nl++;
        if (c == '\n' || c == '\t' || c == ' ')
            state = OUT;
        else if (state == OUT){
            state = IN;
            nw++;
        }
    }
    printf("%d, %d, %d\n", nc, nw, nl);
}


#define MAXHIST 15
#define MAXWORD 11
/*
 * (2) 统计并画出来单词的垂直直方图
 * 比单纯的统计单词个数更进一步，重点在于竖直绘制的开头，要选择打印空格还是*
 * 也是个画状态机的好例子，稍难一些
 */
void printHistgram(){

    int c, i, j, nc ,state;
    int maxvalue;
    int ovflow;
    int wl[MAXWORD];
    state = OUT;
    nc = 0;
    ovflow = 0;
    for (i = 0; i < MAXWORD; i++)
        wl[i] = 0;
    while ((c = getchar()) != EOF){
        if (c == ' ' || c == '\n' || c == '\t'){
            state = OUT;
            if (nc > 0)
                if (nc < MAXWORD)
                    ++wl[nc];
                else
                    ++ovflow;
            nc = 0;
        } else if (state == OUT){
            state = IN;
            nc = 1;
        } else
            ++nc;
    }

    maxvalue = 0;
    for ( i = 1; i < MAXWORD; ++i) {
        if (wl[i] > maxvalue)
            maxvalue = wl[i];
        for(i = MAXHIST; i > 0; --i){
            for (j = 0; j < MAXWORD; ++j) {
                if(wl[j] * MAXHIST / maxvalue >= i)
                    printf(" * ");
                else
                    printf("   ");
            }
            putchar('\n');
        }

        for (i = 1; i < MAXWORD; ++i)
            printf("%4d ", i);
        putchar('\n');
        for (i = 1; i < MAXWORD; ++i)
            printf("%4d ", wl[i]);
        putchar('\n');
        if (ovflow > 0)
            printf(" There are %d words >= %d \n", ovflow, MAXWORD);
    }

}

/*
 * (3)统计数字，空白，其他字符出现的个数
 *  不算什么有质量的程序，有价值的就是统计数字的这点代码而已
 *      ++ndight[c-'0'];
 */
void countChar(){

    int c, i ,nwhite, nother;
    int ndight[10];

    nwhite = nother = 0;
    for (i = 0; i < 10; ++i)
        ndight[i] = 0;

    while ( (c = getchar) != EOF ){
        if (c >= '0' && c <= '9')
            ++ndight[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;
    }

    printf("digits = ");
    for (int j = 0; j < 10; ++j)
        printf(" %d", ndight[i]);
    printf(" , white space = %d, other = %d\n", nwhite, nother);

}


/*
 * (4) 删除C语言的注释
 *
 *
 */



/*
 * (5) 查找括号不匹配的语法错误
 *
 * 注意，出现{时候，brace++，出现}时候，brace--,
 * 所以brace是正数和0都无所谓，当是负数的时候，就意味着出错了
 */



int main() {
    //wordCount();


    return 0;
}
